package com.hc.kotlin

fun main7() {
    //-----List集合-----
    val list = listOf(3, 7, 5) //不可变
    println(list.getOrElse(4) { -1 })
    println(list.getOrNull(5))

    val mutableList = mutableListOf("Jack", "Rose") //可变
    mutableList.add("Nancy")
    println(mutableList)
    mutableList += "Kite" //运算符重载
    mutableList -= "Jack"
    println(mutableList)
    for (s in mutableList) {
        println(s)
    }
    mutableList.forEach {
        println(it)
    }
    mutableList.forEachIndexed { index, s ->
        println(index)
        println(s)
    }
}
fun main() {
    //-----Set集合-----
    val set = setOf(1, 2, 3)
    println(set.elementAtOrElse(4) { -1 })
    val mutableSet = mutableSetOf(5, 6, 7)
    mutableSet.forEach {
        println(it)
    }
    //-----数组-----
    val intArray = intArrayOf(5, 6, 7)
    println(intArray[1])
    intArray.forEach {
        println(it)
    }
    //-----Map-----
    val map = mapOf(1 to 2, 2 to 4, 3 to 1)
    map.forEach{ (a, b) ->
        println("$a-$b")
    }
    println(map.getOrElse(4) { "unknown" })
    println(map.getOrDefault(4,-1))
    val mutableMap = mutableMapOf(1 to 1)
    println(mutableMap)
    mutableMap.getOrPut(2){ 2 }
    println(mutableMap)
}